package com.dao.impl;



import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.dao.OrdersDao;
import com.entity.Cartltem;
import com.entity.Orders;
@Repository
public class OrdersImpl implements OrdersDao{
	@Resource
	private SessionFactory sf;
	
	@Override
	@Transactional
	public int creationOrders(int desk,Orders order) {
			
		order.setDesk(desk);
		order.setState(0);
		order.setPrice(0);
		Date time=new Date();
		System.out.println("时间1："+time);
		order.setOrdersTime(time);		
		this.sf.getCurrentSession().save(order);
		System.out.println("id:"+order.getId());
		return order.getId();
			
	}

	

	@Override
	public boolean pushOrders(Orders orders) {
		Date time=new Date();
		System.out.println("时间2："+time);
		orders.setState(1);
		orders.setEndTime(time);
		this.sf.getCurrentSession().update(orders);
		return true;
	}

	@Override
	public Orders finOrders(int id) {
		Orders orders=this.sf.getCurrentSession().get(Orders.class, id);
		return orders;
	}

	@Override
	public Orders finDeskOrders(int desk) {
		String hql="from Orders where desk=? and state=0";
		Query query=this.sf.getCurrentSession().createQuery(hql);
		query.setParameter(0, desk);
		Orders orders=(Orders)query.uniqueResult();
		return orders;
	}

	@Override
	public List<Cartltem> finall(int orders_id) {
		String hql="from Cartltem where oid=? and state=0";
		Query query=this.sf.getCurrentSession().createQuery(hql);
		query.setParameter(0, orders_id);
		List<Cartltem> list=query.list();
		return list;
	}

}
